package com.agua.enturno.vista.paginas.prestador;

import com.agua.enturno.vista.componentes.HorarioGUI;
import com.agua.enturno.vista.componentes.WeekDayLabel;
import com.agua.enturno.vista.dto.HorarioDTO;
import com.agua.enturno.vista.eventos.CambioDeSemanaEvent;
import com.agua.enturno.vista.util.SemanasIterator;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.joda.time.LocalDate;

import java.util.Arrays;
import java.util.List;

@SuppressWarnings("unused")
public class AgendaPanel extends Panel {

	private static final long serialVersionUID = 1L;

    private WebMarkupContainer horariosContainer;
    private WebMarkupContainer diasContainer;
    
    public AgendaPanel(String id, SemanasIterator semanasIterator) {
		super(id);
		this.setOutputMarkupId(true);
		
		addDiasDeLaSemana(semanasIterator);
		addHorasDelDia();
		addHorarios(semanasIterator);
	}
    
	private void addDiasDeLaSemana(SemanasIterator semanasIterator) {
		
		diasContainer = new WebMarkupContainer("dias-container");
		diasContainer.setOutputMarkupId(true);
		diasContainer.add(new WeekDayLabel("dia-lunes",     new PropertyModel<LocalDate>(semanasIterator, "semanaActual.lunes.fecha")));
		diasContainer.add(new WeekDayLabel("dia-martes",    new PropertyModel<LocalDate>(semanasIterator, "semanaActual.martes.fecha")));
		diasContainer.add(new WeekDayLabel("dia-miercoles", new PropertyModel<LocalDate>(semanasIterator, "semanaActual.miercoles.fecha")));
		diasContainer.add(new WeekDayLabel("dia-jueves",    new PropertyModel<LocalDate>(semanasIterator, "semanaActual.jueves.fecha")));
		diasContainer.add(new WeekDayLabel("dia-viernes",   new PropertyModel<LocalDate>(semanasIterator, "semanaActual.viernes.fecha")));
		diasContainer.add(new WeekDayLabel("dia-sabado",    new PropertyModel<LocalDate>(semanasIterator, "semanaActual.sabado.fecha")));
		diasContainer.add(new WeekDayLabel("dia-domingo",   new PropertyModel<LocalDate>(semanasIterator, "semanaActual.domingo.fecha")));
		add(diasContainer);
	}
	
	private void addHorasDelDia() {
		add(new ListView<String>("hora-del-dia", getHorasDelDia()) {
			private static final long serialVersionUID = 1L;
			@Override
			protected void populateItem(ListItem<String> item) {
				final String hora = item.getModelObject();
				item.add(new Label("hora-del-dia-interno", item.getModelObject()) {
					private static final long serialVersionUID = 1L;
					@Override
					protected void onComponentTag(ComponentTag tag) {
						super.onComponentTag(tag);
						tag.put("style", "height: " + 60 * HorarioGUI.MINUTE_HEIGHT + "px;");
						tag.put("title", hora);
					}
				});
			}
		});
	}

	private void addHorarios(SemanasIterator semanasIterator) {
		
		horariosContainer = new WebMarkupContainer("horarios-container");
		horariosContainer.setOutputMarkupId(true);
		horariosContainer.add(new DiaDeAgendaGUI("dia-lunes",     new PropertyModel<List<HorarioDTO>>(semanasIterator, "semanaActual.lunes.horarios")));
		horariosContainer.add(new DiaDeAgendaGUI("dia-martes",    new PropertyModel<List<HorarioDTO>>(semanasIterator, "semanaActual.martes.horarios")));
		horariosContainer.add(new DiaDeAgendaGUI("dia-miercoles", new PropertyModel<List<HorarioDTO>>(semanasIterator, "semanaActual.miercoles.horarios")));
		horariosContainer.add(new DiaDeAgendaGUI("dia-jueves",    new PropertyModel<List<HorarioDTO>>(semanasIterator, "semanaActual.jueves.horarios")));
		horariosContainer.add(new DiaDeAgendaGUI("dia-viernes",   new PropertyModel<List<HorarioDTO>>(semanasIterator, "semanaActual.viernes.horarios")));
		horariosContainer.add(new DiaDeAgendaGUI("dia-sabado",    new PropertyModel<List<HorarioDTO>>(semanasIterator, "semanaActual.sabado.horarios")));
		horariosContainer.add(new DiaDeAgendaGUI("dia-domingo",   new PropertyModel<List<HorarioDTO>>(semanasIterator, "semanaActual.domingo.horarios")));
		add(horariosContainer);
	}
	
	@Override
	public void onEvent(IEvent<?> event) {
		super.onEvent(event);
		
        if (event.getPayload() instanceof CambioDeSemanaEvent) {
        	CambioDeSemanaEvent cambioDeSemanaEvent = (CambioDeSemanaEvent) event.getPayload();
        	cambioDeSemanaEvent.getTarget().add(diasContainer);
        	cambioDeSemanaEvent.getTarget().add(horariosContainer);
        	cambioDeSemanaEvent.getTarget().appendJavaScript("$('#" + horariosContainer.getMarkupId() + "').effect('pulsate', {times: 1}, 500);");
        }
	}
	
	private List<String> getHorasDelDia() {
		return Arrays.asList(
				"00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", 
				"12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00"
		);
	}

}
